package net.chrone.quickpayapi.util;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;


public class RedisClient {

	private static Jedis jedis;//非切片客户端连接,用于单机redis
	private static JedisPool jedisPool;//非切片客户端连接池,用于单机redis
	
	private static final String REDIS_IP = SdkUtil.getStringValue("redisIp");
	private static final Integer REDIS_PORT = SdkUtil.getIntValue("redisPort");
	private static final String REDIS_PASSWORD = SdkUtil.getStringValue("redisPasswd");
	
	public static final String CACHE_PREFIX_ID = "quick_pay_api_id_";
	public static final String CACHE_PREFIX_CITY = "quick_pay_api_city_";
	public static final String CACHE_PREFIX_ROOT_BANK = "quick_pay_api_rbank_";
	public static final String CACHE_PREFIX_PMS_BANK = "quick_pay_api_pbank_";
	
	
	static{
		initalPool();//初始化连接池
		jedis = jedisPool.getResource();
		jedis.auth(REDIS_PASSWORD);
	}
	
	

	private static void initalPool() {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(2000);
		config.setMaxIdle(10);
		config.setMaxWaitMillis(10000);
		config.setTestOnBorrow(false);
		jedisPool = new JedisPool(config,REDIS_IP,REDIS_PORT);
	}
	
	public  static Jedis getClient(){
		jedis.select(0);
		return jedis;
	}
	
	
	
	public synchronized static String getByKey(String key){
		return jedis.get(key);
	}
	
	public synchronized static Long inc(String key){
		return jedis.incr(key);
	}
	
	public synchronized static String set(String key,String value){
		 return jedis.set(key, value);
	}
	
	public synchronized static Boolean exisit(String key){
		return jedis.exists(key);
	}
	
}
